package com.wooming.myserver.service.impl;

import com.wooming.myserver.constant.ModuleName;
import com.wooming.myserver.dto.GameDataDTO;
import com.wooming.myserver.dto.GameStatusDTO;
import com.wooming.myserver.dto.HumanResourceDTO;
import com.wooming.myserver.entity.HumanResource;
import com.wooming.myserver.entity.ModuleStatus;
import com.wooming.myserver.mapper.GameDataMapper;
import com.wooming.myserver.service.inter.GameInitService;
import com.wooming.myserver.util.Cost;
import com.wooming.myserver.vo.AllCostVO;
import com.wooming.myserver.vo.StartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 初始化用户游戏服务实现类
 *
 * @author Wooming 2025/05/23
 */
@Service
public class GameInitServiceImpl implements GameInitService {
    // 依赖项
    private final GameDataMapper gameDataMapper;

    // 构造函数注入
    @Autowired
    public GameInitServiceImpl(GameDataMapper gameDataMapper) {
        this.gameDataMapper = gameDataMapper;
    }

    @Override
    public boolean init(Integer userId) {
        try {
            // 初始化用户基础数据
            gameDataMapper.addNewUserToGameData(userId);
            // 初始化用户统计数据
            gameDataMapper.addNewUserGameTotal(userId);
            // 初始化用户模块状态数据
            // 前端模块
            gameDataMapper.addNewUserGameStatus(userId, ModuleName.FRONT_UI.getName());
            gameDataMapper.addNewUserGameStatus(userId, ModuleName.FRONT_INTERACT.getName());
            gameDataMapper.addNewUserGameStatus(userId, ModuleName.FRONT_FRAMEWORK.getName());
            // 后端模块
            gameDataMapper.addNewUserGameStatus(userId, ModuleName.BACK_SPEED.getName());
            gameDataMapper.addNewUserGameStatus(userId, ModuleName.BACK_COMM.getName());
            gameDataMapper.addNewUserGameStatus(userId, ModuleName.BACK_FRAMEWORK.getName());
            // 安全模块
            gameDataMapper.addNewUserGameStatus(userId, ModuleName.SECURITY_DDOS.getName());
            gameDataMapper.addNewUserGameStatus(userId, ModuleName.SECURITY_SQL.getName());
            gameDataMapper.addNewUserGameStatus(userId, ModuleName.SECURITY_XSS.getName());
        } catch (Exception e) {
            System.err.println(e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public StartVO start(Integer userId) {
        StartVO startVO = new StartVO();
        // 获取基本数据
        GameDataDTO gameDataDTO = gameDataMapper.getGameData(userId);
        startVO.setGold(gameDataDTO.getGold());
        startVO.setActionPoint(gameDataDTO.getActionPoint());
        startVO.setReputation(gameDataDTO.getReputation());
        startVO.setTraffic(gameDataDTO.getTraffic());
        startVO.setAvgConsumption(gameDataDTO.getAvgConsumption());
        startVO.setTurns(gameDataDTO.getTurn());
        startVO.setStage(gameDataDTO.getLevel());

        // 获取模块状态信息
        List<GameStatusDTO> gameStatusDTOList = gameDataMapper.getGameStatus(userId);
        startVO.setModuleStatuses( // 流式转换
                gameStatusDTOList.stream()
                        .map(this::change)
                        .toList()
        );

        // 获取人力资源信息
        List<HumanResourceDTO> humanResourceDTOList = gameDataMapper.getHumanResource(userId);
        startVO.setHumanResources( // 流式转换
                humanResourceDTOList.stream()
                        .map(this::change)
                        .toList()
        );
        return startVO;
    }

    private ModuleStatus change(GameStatusDTO data) {
        return new ModuleStatus(
                data.getModuleType(),
                data.getCurrentLevel()
        );
    }

    private HumanResource change(HumanResourceDTO data) {
        return new HumanResource(
                data.getId(),
                data.getName(),
                data.getLevel(),
                data.getMaxLevel(),
                data.getTrainCost(),
                data.getActionPoint(),
                data.getSalary()
        );
    }

    @Override
    public AllCostVO allCost(Integer userId) {
        AllCostVO allCostVO = new AllCostVO();

        // 获取全部模块升级所需的资源消耗
        setAll(userId, allCostVO);

        // 返回
        return allCostVO;
    }

    private void setAll(Integer userId, AllCostVO allCostVO) {
        // 前端模块
        GameStatusDTO frameworkStatus = gameDataMapper.getModuleStatus(userId, ModuleName.FRONT_FRAMEWORK.getName());
        GameStatusDTO status = gameDataMapper.getModuleStatus(userId, ModuleName.FRONT_UI.getName());
        Cost cost = Cost.getCost(status, frameworkStatus);
        allCostVO.setFrontUIGold(cost.goldCost());
        allCostVO.setFrontUIAction(cost.actionCost());
        status = gameDataMapper.getModuleStatus(userId, ModuleName.FRONT_INTERACT.getName());
        cost = Cost.getCost(status, frameworkStatus);
        allCostVO.setFrontInteractGold(cost.goldCost());
        allCostVO.setFrontInteractAction(cost.actionCost());
        status = gameDataMapper.getModuleStatus(userId, ModuleName.FRONT_FRAMEWORK.getName());
        cost = Cost.getCost(status, frameworkStatus);
        allCostVO.setFrontFrameworkGold(cost.goldCost());
        allCostVO.setFrontFrameworkAction(cost.actionCost());
        // 后端模块
        frameworkStatus = gameDataMapper.getModuleStatus(userId, ModuleName.BACK_FRAMEWORK.getName());
        status = gameDataMapper.getModuleStatus(userId, ModuleName.BACK_SPEED.getName());
        cost = Cost.getCost(status, frameworkStatus);
        allCostVO.setBackSpeedGold(cost.goldCost());
        allCostVO.setBackSpeedAction(cost.actionCost());
        status = gameDataMapper.getModuleStatus(userId, ModuleName.BACK_COMM.getName());
        cost = Cost.getCost(status, frameworkStatus);
        allCostVO.setBackCommGold(cost.goldCost());
        allCostVO.setBackCommAction(cost.actionCost());
        status = gameDataMapper.getModuleStatus(userId, ModuleName.BACK_FRAMEWORK.getName());
        cost = Cost.getCost(status, frameworkStatus);
        allCostVO.setBackFrameworkGold(cost.goldCost());
        allCostVO.setBackFrameworkAction(cost.actionCost());
        // 安全模块
        status = gameDataMapper.getModuleStatus(userId, ModuleName.SECURITY_DDOS.getName());
        cost = Cost.getCost(status, frameworkStatus);
        allCostVO.setSecurityDDoSGold(cost.goldCost());
        allCostVO.setSecurityDDoSAction(cost.actionCost());
        status = gameDataMapper.getModuleStatus(userId, ModuleName.SECURITY_SQL.getName());
        cost = Cost.getCost(status, frameworkStatus);
        allCostVO.setSecuritySQLGold(cost.goldCost());
        allCostVO.setSecuritySQLAction(cost.actionCost());
        status = gameDataMapper.getModuleStatus(userId, ModuleName.SECURITY_XSS.getName());
        cost = Cost.getCost(status, frameworkStatus);
        allCostVO.setSecurityXSSGold(cost.goldCost());
        allCostVO.setSecurityXSSAction(cost.actionCost());
    }
}
